<template>
  <div class="timeline-item">
    <!-- 日期标志 -->
    <div class="date-marker">
      <div class="date-dot"></div>
      <div class="date-text">{{ formatMonth(item.date) }}.{{ formatDay(item.date) }}</div>
      <div class="date-line"></div>
    </div>

    <!-- 文章卡片 -->
    <router-link :to="item.link" class="post-card">
      <div class="post-info">
        <div class="post-category" :style="{ background: item.categoryColor }">
          {{ item.category }}
        </div>
        <h3 class="post-title">{{ item.title }}</h3>
        <p class="post-desc">{{ item.description }}</p>
      </div>

      <div class="read-more">
        阅读全文
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
        >
          <path d="M5 12h14"></path>
          <path d="M12 5l7 7-7 7"></path>
        </svg>
      </div>
    </router-link>
  </div>
</template>

<script setup>
defineProps({
  item: {
    type: Object,
    required: true,
  },
})

// 格式化日期 - 月份
const formatMonth = (dateStr) => {
  const date = new Date(dateStr)
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  return month
}

// 格式化日期 - 日
const formatDay = (dateStr) => {
  const date = new Date(dateStr)
  const day = date.getDate().toString().padStart(2, '0')
  return day
}

defineOptions({ name: 'TimelineItem' })
</script>

<style lang="scss" scoped>
.timeline-item {
  display: flex;
  margin-bottom: 2.5rem;
  position: relative;

  &:last-child {
    margin-bottom: 0;
  }

  // 日期标志
  .date-marker {
    flex-shrink: 0;
    width: 5rem;
    display: flex;
    flex-direction: row;
    align-items: center;
    position: relative;
    z-index: 1;

    .date-dot {
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background-color: var(--color-primary);
      position: relative;
      margin-right: 0.75rem;

      &::after {
        content: '';
        position: absolute;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        border: 1px solid var(--border-base);
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
    }

    .date-text {
      font-size: 0.875rem;
      font-weight: 500;
      color: var(--text-secondary);
      margin-right: 0.75rem;
    }

    .date-line {
      flex: 1;
      height: 1px;
      background: linear-gradient(to right, var(--border-base), transparent);
    }
  }

  // 文章卡片
  .post-card {
    flex: 1;
    background-color: var(--bg-base);
    border-radius: 0.75rem;
    border: 1px solid var(--border-base);
    margin-left: 1rem;
    position: relative;
    text-decoration: none;
    transition: all 0.25s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    &:hover {
      transform: translateY(-3px);
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
      border-color: transparent;

      .read-more {
        svg {
          transform: translateX(4px);
        }
      }
    }

    .post-info {
      padding: 1.25rem 1.5rem 0.75rem;
      flex: 1;

      // 分类标签
      .post-category {
        display: inline-block;
        padding: 0.25rem 0.625rem;
        font-size: 0.75rem;
        font-weight: 600;
        color: white;
        border-radius: 2rem;
        margin-bottom: 0.75rem;
      }

      // 文章标题
      .post-title {
        font-size: 1.125rem;
        font-weight: 700;
        line-height: 1.4;
        margin-bottom: 0.625rem;
        color: var(--text-primary);
      }

      // 文章描述
      .post-desc {
        font-size: 0.875rem;
        line-height: 1.6;
        color: var(--text-secondary);
        margin-bottom: 0.5rem;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
    }

    // 阅读全文
    .read-more {
      font-size: 0.875rem;
      font-weight: 500;
      color: var(--color-primary);
      display: flex;
      align-items: center;
      gap: 0.375rem;
      padding: 0.75rem 1.5rem;
      border-top: 1px solid var(--border-base);
      background-color: rgba(81, 138, 255, 0.03);

      svg {
        transition: transform 0.2s ease;
      }
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .timeline-item {
    margin-bottom: 1.5rem;
    flex-direction: column; // 在移动端垂直布局

    .date-marker {
      width: 100%;
      height: auto;
      margin-bottom: 0.75rem;
      padding-left: 2rem;

      .date-dot {
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
      }

      .date-text {
        font-size: 0.8125rem;
        color: var(--color-primary);
      }

      .date-line {
        display: none;
      }
    }

    .post-card {
      margin-left: 0;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

      .post-info {
        padding: 1rem 1rem 0.5rem;

        .post-category {
          padding: 0.175rem 0.5rem;
          margin-bottom: 0.5rem;
          font-size: 0.7rem;
        }

        .post-title {
          font-size: 1rem;
          line-height: 1.4;
          margin-bottom: 0.5rem;
        }

        .post-desc {
          font-size: 0.8125rem;
          line-height: 1.5;
          -webkit-line-clamp: 2; // 限制在两行
        }
      }

      .read-more {
        padding: 0.5rem 1rem;
        font-size: 0.8125rem;
        justify-content: space-between;
      }
    }
  }
}

@media (max-width: 480px) {
  .timeline-item {
    .date-marker {
      padding-left: 1.5rem;

      .date-dot {
        width: 6px;
        height: 6px;

        &::after {
          width: 14px;
          height: 14px;
        }
      }

      .date-text {
        font-size: 0.75rem;
      }
    }

    .post-card {
      .post-info {
        padding: 0.875rem 0.875rem 0.375rem;

        .post-title {
          font-size: 0.9375rem;
        }

        .post-desc {
          font-size: 0.75rem;
          margin-bottom: 0.375rem;
        }
      }

      .read-more {
        padding: 0.4375rem 0.875rem;
        font-size: 0.75rem;
      }
    }
  }
}

// 暗色模式适配
:deep(html.dark) {
  .timeline-item {
    .post-card {
      background-color: var(--bg-light);
      border-color: rgba(255, 255, 255, 0.1);

      .read-more {
        background-color: rgba(81, 138, 255, 0.1);
        border-color: rgba(255, 255, 255, 0.1);
      }
    }
  }
}
</style>
